﻿using RFIDSystem.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RFIDSystem
{
    public partial class UserManage : BaseForm
    {
        public UserManage()
        {
            InitializeComponent();
        }

        

        private void UserManage_Load(object sender, EventArgs e)
        {
            BindDropDownList();
            BindUserList();
        }

        private void BindDropDownList()
        {
            var role = DAL.roles.ToList();
            this.cbRole.DisplayMember = "role_name";
            this.cbRole.ValueMember = "role_id";
            this.cbRole.DataSource = role;
        }

        private void BindUserList()
        {
            var users = from u in DAL.users
                           select new
                           {
                               u.user_id,
                               u.username,
                               u.password,
                               u.role.role_name,
                           };
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = users.ToList();
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewRow row = dataGridView1.CurrentRow;
            cbRole.SelectedIndex = cbRole.FindString(row.Cells["role_name"].Value.ToString());

            txtUserName.Text = row.Cells["username"].Value.ToString();
            txtPassword.Text = row.Cells["password"].Value.ToString();
            
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var role = new role();
            var user = new user();

            role = DAL.roles.Where(u => u.role_id == (int)cbRole.SelectedValue).FirstOrDefault();

            user.role = role;
            user.username = txtUserName.Text;
            user.password = txtPassword.Text;

            DAL.users.Add(user);
            DAL.SaveChanges();
            BindUserList();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            int currentId = (int)dataGridView1.CurrentRow.Cells["id"].Value;
            var user = DAL.users.Find(currentId);

            var role = DAL.roles.Where(r => r.role_id == (int)cbRole.SelectedValue).FirstOrDefault();

            user.role = role;
            user.username = txtUserName.Text;
            user.password = txtPassword.Text;

            DAL.SaveChanges();
            BindUserList();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int currentId = (int)this.dataGridView1.CurrentRow.Cells["id"].Value;
            var user = DAL.users.Find(currentId);

            if (user.username=="admin")
            {
                MessageBox.Show("admin为系统级别用户，不能删除");
                return;
            }

            DAL.users.Remove(user);
            DAL.SaveChanges();
            BindUserList();
        }
    }
}
